import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-device-log',
	templateUrl: './device-log.component.html',
})
export class DeviceLogComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	isShowDelete=false; // 默认不显示删除按钮
	// 查询参数
	deviceStatus:any = ""; // 设备状态
	bluetoothStatus:any = ""; // 蓝牙状态
    selectedDeviceId: any = ""; // 选中的设备ID
    selectedCarNumber: any = ""; // 选中的车牌号
    selectedCompanyId: any = "";  // 公司Id
	selectedCompanyName: any = ""; // 公司名称
	selectStatusIndex:any = ""; // 选择的硬件状态的大类的index
	selectStatusContent:any = ""; // 选择的硬件状态的文字
    startTime: any = ""; // 开始时间
    endTime: any = ""; // 结束时间
	searchKey: any = ""; // 用户的搜索关键字
	userName:any = ""; // 操作人
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	status: any = [{
        id: '3',
        label: 'GPS天线状态',
        children: [{
            id: '0101',
            label: '异常',
        }, {
            id: '0102',
            label: '正常',
        }, {
            id: '0103',
            label: '未知',
        }, {
            id: '0104',
            label: '开路',
        }, {
            id: '0105',
            label: '短路',
        }, {
            id: '0106',
            label: '参数错误',
        }]
    }, {
        id: '2',
        label: 'GPRS状态',
        children: [{
            id: '0201',
            label: '初始化',
        }, {
            id: '0202',
            label: '已连接服务器',
        }, {
            id: '0203',
            label: 'AT串口通讯错误',
        }, {
            id: '0204',
            label: '模块未识别',
        }, {
            id: '0205',
            label: 'SIM卡未插入或未识别',
        }, {
            id: '0206',
            label: 'GPRS网络信号强度过低',
        }, {
            id: '0207',
            label: 'SIM卡注册失败',
        }, {
            id: '0208',
            label: 'GPRS网络附着失败',
        }, {
            id: '0209',
            label: 'PPP拨号连接失败',
        }, {
            id: '0210',
            label: '与服务器建立TCP连接失败',
        }]
    }];
	ngOnInit() {
		// this.initBtn();
		this.initDate();
		this.initSelect2();
		this.load();
	}
	//初始化权限按钮
	// initBtn() {
	// 	var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_wx:bind:deviceLog')));
	// 	if(!btnData) {
	// 		return
	// 	};
	// 	for(let item of btnData) {
	// 		if(item.sourceKey === "log-info:device-log:deleteBtn" && item.parentId) {
	// 			this.isShowDelete = true;
	// 		}
		
	// 	}
	// }
	// 初始化查询的select 2
	initSelect2() {
		let that = this;
        System.import('script-loader!select2/dist/js/select2.min.js').then(() => {
            System.import('assets/js/select2-zh-CN.js').then(() => {
                $("#statusSelect2").select2({language: "zh-CN"}).on("select2:select", function (e) {
					var data = e.params.data;
					that.selectStatusIndex = data.id;
					that.selectStatusContent = data.text=="全部"?"":data.text
                })
            })
        })
    }
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId:this.selectedCompanyId,
			    deviceStatus : this.deviceStatus,
                searchKey:common.stringTrim(this.searchKey),
				deviceId : this.selectedDeviceId,
				index : this.selectStatusIndex,
				content:this.selectStatusContent,
				bluetoothStatus : this.bluetoothStatus,
				startTime : this.startTime,
				endTime : this.endTime,
				userName:this.userName.trim()
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		// this.jsonApiService.http("repairCheck/list.html", reqdata).subscribe(data => {
		this.jsonApiService.http("deviceCheck/list.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				for (let index = 0; index < data.exhibitDatas.length; index++) {
					const ele = data.exhibitDatas[index].deviceCheckInfo;
					for (let ind = 0; ind < ele.length; ind++) {
						const device = ele[ind];
						if(device.index=="4"){
							for (let i = 0; i < device.deviceCheckSubInfo.length; i++) {
								const dev = device.deviceCheckSubInfo[i];
								dev.content = dev.content.split(",")
							}
						}
					}
				}
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
     //公司选中回调
	 companySelected(data) {
		this.selectedDeviceId = data.deviceId;
		this.selectedCarNumber = data.carNumber;
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
	 // 时间选择回调
	dateSelect(time) {
        this.startTime = time.startTime;
		this.endTime = time.endTime;
		if (time.startTime == "" || time.endTime == "") {
			this.dateNumber = 0;
		}
	}
	// 初始化1天的时间
	initDate(){
		let nowTime = new Date();
		this.endTime = common.getTimeMmss(nowTime);
		nowTime.setDate(nowTime.getDate() - 1);
		this.startTime = common.getTimeMmss(nowTime);
		this.specifyTime = {
			startTime: this.startTime,
			endTime: this.endTime,
		}
	}
	dateNumber = 0; //查看天数
	specifyTime: any;
	selectDate() {
		let nowTime = new Date();
		this.endTime = common.getTimeMmss(nowTime);
		nowTime.setDate(nowTime.getDate() - this.dateNumber);
		this.startTime = common.getTimeMmss(nowTime);
		this.specifyTime = {
			startTime: this.startTime,
			endTime: this.endTime,
		}
	}
	// 删除设备自检记录
	deleteRow(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("repairCheck/batchDeleted.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	// 数据导出
	exportData(){
		const param = {
			companyId:this.selectedCompanyId,
			deviceStatus : this.deviceStatus,
			searchKey:common.stringTrim(this.searchKey),
			deviceId : this.selectedDeviceId,
			index : this.selectStatusIndex,
			content:this.selectStatusContent,
			bluetoothStatus : this.bluetoothStatus,
			startTime : this.startTime,
			endTime : this.endTime,
			userName:this.userName.trim()
		}
		let timeCheck = common.correctTime(param)
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(param, 'param', config.BaseURL + 'deviceCheck/exportDeviceCheckData.html')		
    }
}